﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;
using PlatypusPlanner.Components.Model;
using PlatypusPlanner.Components.Utilities;


namespace PlatypusPlanner.Components.Utilities
{
    public class XMLParser
    {
        public static Message GetMessage(int messageId)
        {
            string key = string.Format("MESSAGE{0}", messageId);

            if (CacheUtility.GetFromCache<Message>(key) != null)
                return CacheUtility.GetFromCache<Message>(key) as Message;

            XmlDocument doc = new XmlDocument();
            doc.Load(HttpContext.Current.Server.MapPath(GlobalURLPaths.XMLMessagePath));

            XmlNodeList list = doc.SelectNodes("//message");
            foreach (XmlNode node in list)
            {
                if (node.Attributes.Count > 0)
                    if (int.Parse(node.Attributes["id"].Value) == messageId)
                    {
                        Message msg = new Message(node);
                        CacheUtility.AddToCache(key, msg, Globals.CACHE_EXPIRE_TIME);
                        return msg;
                    }
            }

            return null;
        }
    }
}